#!/usr/bin/python

__author__ = 'elsigh@google.com (Lindsey Simon)'

import logging

from lib import resource

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class Handler(webapp.RequestHandler):
  def get(self):
    params = resource.ParseParams(self.request)

    headers = resource.GenHeaders(self.request, params)
    for key, val in headers.items():
      self.response.headers[key] = val

    # Status seems to need to be set via set_status on prod.
    if params.has_key('status'):
      logging.info('status: %s' % params['status'])
      status_text = None
      if resource.STATUS_TEXT.has_key(params['status']):
        status_text = resource.STATUS_TEXT[params['status']]
      logging.info('status_text: %s' % status_text)
      self.response.set_status(params['status'], status_text)

    content = resource.GenContent(self.request, params)
    self.response.out.write(content)


app = webapp.WSGIApplication([('/', Handler),
                              ('/resource.py', Handler)],
                             debug=True)


def main():
  run_wsgi_app(app)


if __name__ == "__main__":
  main()
